package com.atguigu.flink0624.chapter07.state;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import org.apache.flink.streaming.connectors.kafka.KafkaSerializationSchema;
import org.apache.flink.util.Collector;
import org.apache.kafka.clients.producer.ProducerRecord;

import javax.annotation.Nullable;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/11/15 15:25
 */
public class Flink11_Kafka_Flink_Kafka {
    
    public static void main(String[] args) throws Exception {
        System.setProperty("HADOOP_USER_NAME", "atguigu"); // shift+ctrl+u
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        env.enableCheckpointing(3000);
        env.setStateBackend(new HashMapStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/Flink11_Kafka_Flink_Kafka");
        
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        // 确认 checkpoints 之间的时间会进行 500 ms
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);
    
        // Checkpoint 必须在一分钟内完成，否则就会被抛弃
        env.getCheckpointConfig().setCheckpointTimeout(60000);
    
        // 同一时间只允许最多一个 checkpoint 进行
        env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);
    
        // 开启在 job 中止后仍然保留的 externalized checkpoints
        env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
    
        
        Properties sourceConfig = new Properties();
        sourceConfig.put("bootstrap.servers", "hadoop162:9092,hadoop163:9092,hadoop164:9092");
        sourceConfig.put("group.id", "Flink11_Kafka_Flink_Kafka_3");
        sourceConfig.put("auto.offset.reset", "latest"); // 开启checkpoint之后, 如果没有消费记录, 从这个配置开始消费. 如果有消费记录, 则是从上次的记录的位置开始消费
        sourceConfig.put("isolation.level", "read_committed");
        
        Properties sinkConfig = new Properties();
        sinkConfig.put("bootstrap.servers", "hadoop162:9092,hadoop163:9092,hadoop164:9092");
        sinkConfig.put("transaction.timeout.ms", 15 * 60 * 1000);
    
        SingleOutputStreamOperator<Tuple2<String, Long>> stream = env
            .addSource(new FlinkKafkaConsumer<String>("s1", new SimpleStringSchema(), sourceConfig))
            .flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {
                @Override
                public void flatMap(String value, Collector<Tuple2<String, Long>> out) throws Exception {
                    for (String word : value.split(" ")) {
                        out.collect(Tuple2.of(word, 1L));
                    
                    }
                }
            })
            .keyBy(t -> t.f0)
            .sum(1);
        
        stream
            .addSink(new FlinkKafkaProducer<Tuple2<String, Long>>(
                "default",
                new KafkaSerializationSchema<Tuple2<String, Long>>() {
                    @Override
                    public ProducerRecord<byte[], byte[]> serialize(Tuple2<String, Long> element,
                                                                    @Nullable Long timestamp) {
                        return new ProducerRecord<byte[], byte[]>("s2", (element.f0 + "_" + element.f1).getBytes(StandardCharsets.UTF_8));
                    }
                },
                sinkConfig,
                FlinkKafkaProducer.Semantic.EXACTLY_ONCE)  //开启2阶段提交
            );
        
        stream.addSink(new SinkFunction<Tuple2<String, Long>>() {
            @Override
            public void invoke(Tuple2<String, Long> value, Context context) throws Exception {
                if (value.f0.contains("x")) {
                    throw new RuntimeException();
                }
            }
        });
        
        
        
        env.execute();
        
    }
    
}
